package dk.eclipse.android.badassapp;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
import dk.eclipse.android.badassapp.helper.SoundHelper;
import dk.eclipse.android.badassapp.preferences.PreferenceHelper;
import dk.eclipse.android.badassapp.weapon.WeaponHelper;

/**
 * This class presents a user interface for the splash.
 * 
 * @author Ronni Kahalani (eclipse.dk)
 * 
 */
public class SplashScreenActivity extends Activity {
	/**
	 * Tag used for logging information.
	 */
	private static final String LOG_TAG = SplashScreenActivity.class
			.getSimpleName();

	/**
	 * The remark strings.
	 */
	int[] remarkIds = { R.string.splashRemark1, R.string.splashRemark2,
			R.string.splashRemark3, R.string.splashRemark4,
			R.string.splashRemark5 };
	/**
	 * Holds the current progress information.
	 */
	private TextView progressInfo;
	/**
	 * Holds the remark.
	 */
	private TextView remarkInfo;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.splash);
		setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
				R.drawable.ic_app);

		progressInfo = (TextView) findViewById(R.id.progressTextView);
		remarkInfo = (TextView) findViewById(R.id.remarkTextView);

		remarkInfo.setText( getRandomRemark());

		// thread for displaying the SplashScreen
		Thread splashTread = new Thread() {

			@Override
			public void run() {

				Context context = getApplicationContext();
				try {
					SoundHelper.playSound(context, R.raw.grunt, false);

					WeaponHelper.setContext(context);

					if (PreferenceHelper.getCreateDatabase(context)) {
						setInfo("Deleting existing database...");
						WeaponHelper.deleteDb();
					}
					if (WeaponHelper.isDbAvailable()) {
						setInfo("Loading weapons...");
					} else {
						setInfo("Creating new weapons database...");
					}

					WeaponHelper.loadDatabase();

					setInfo("Preparing weapons...");
					Thread.sleep(500);

					setInfo("Weapons loaded");
					Thread.sleep(500);

				} catch (Exception e) {
					// do nothing
					e.printStackTrace();
				} finally {
					
					SoundHelper.stopSound(context, R.raw.grunt);
					finish();
					SoundHelper.playSound(context, R.raw.door, false);

					Intent intent = new Intent(context, BadAssAppActivity.class);
					startActivity(intent);
				}
			}
		};

		splashTread.start();
	}

	/**
	 * Sets the progress info, via UI thread.
	 * 
	 * @param info
	 */
	private void setInfo(final String info) {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				progressInfo.setText(info);
			}
		});
	}


	/**
	 * Returns a random number between min and max, incl.
	 * 
	 * @param min
	 * @param max
	 * @return the random number
	 */
	private int getRandomInt(int min, int max) {
		Random randomizer = new Random(System.currentTimeMillis());
		return randomizer.nextInt(max - min + 1) + min;
	}

	/**
	 * Returns a random remark.
	 * 
	 * @return the remark
	 */
	private String getRandomRemark() {
		return getString( remarkIds[ getRandomInt(0, remarkIds.length - 1)]);
	}

	@Override
	public void onDestroy() {

		SoundHelper.stopSound(getApplicationContext(), R.raw.grunt);
		super.onDestroy();
	}
}